This sample demonstrates how you can enqueue and dequeue messages from your Windows Store apps into a Windows Azure Service Bus Queue by using Windows Azure Mobile Services.
This code sample builds out an ordering scenario with both a Sales and Storeroom app. The idea of the scenario is to showcase a simple internal retail store application that communicates using two different applications by using a Windows Azure Service Bus queue to send and receive product orders:
- The
Salesapp inserts a new order. - The
Ordertable's insert script enqueues a Service Bus message including the order info. - A scheduled job dequeues the Service Bus message and sends a push notification to the registered channels.
- The
Storeroomapp receives the push notification and refreshes the orders list.
To obtain an evaluation copy of Windows 8.1, go to Windows 8.1.
To obtain an evaluation copy of Microsoft Visual Studio 2013, go to Visual Studio 2013.
Note For Windows 8 app samples, download the Windows 8 app samples pack. The samples in the Windows 8 app samples pack will build and run only on Microsoft Visual Studio 2012.
Related topics
Operating system requirements
| Client | |
|---|---|
| Server |
Build the sample
- If you do not have a Windows Azure subscription, sign up for a free Windows Azure trial.
- Install the Windows Azure command line tools.
- Open a command prompt to download the required credentials to communicate with Windows Azure as follows. This is a one-time setup for running all subsequent commands to manage Mobile Services:
- Download Windows Azure management credentials with this command:
azure account download. This will display a web page for you to sign in to the Windows Azure management portal. After you sign in, Windows Azure will prompt you to download a publish settings file for your Windows Azure subscription. Save this file to your local computer. - Import the publish settings file from this location with this command:
azure account import[SavedLocation]. This will configure your command prompt to manage all of your Windows Azure services from the command line.
- Download Windows Azure management credentials with this command:
- Create a Windows Azure Mobile Service with this command:
azure mobile create[AzureMobileServiceName] [sqlAdminUsername] [sqlAdminPassword] - Create a Windows Azure Service Bus namespace where you will create a queue to store and process orders:
- Get available Windows Azure Service Bus datacenter locations with this command:
azure sb namspace location list. - Create the namespace with this command:
azure sb namespace create[ServiceBusNamspaceIdentifier] [ServiceBusLocation]. Make a note of theServiceBusNamespaceIdentifier(provided by you) and the default key (generated in output) for your created namespace.
- Get available Windows Azure Service Bus datacenter locations with this command:
- Sign in to the Windows Store Dashboard and register your app to receive push notifications.
- In Microsoft Visual Studio, open the sample solution, right-click the
Storeroomproject, click Store > Associate app with the Store, and follow the on-screen directions to sign in and then select the app that you previously registered in the Windows Store Dashboard. - Sign in to the Live Connect app management site and select your app in the My applications list. With your app showing, click Edit settings. Under API settings, make a note of the values under Client secret and Package security identifier (SID).
- Configure your Windows Azure Mobile Service with the client secret and package SID values with these commands:
azure mobile config set[AzureMobileServiceName]microsoftAccountClientSecret[ClientSecret]azure mobile config set[AzureMobileServiceName]microsoftAccountPackageSID[PackageSID]
- Create a
Channeltable that will be used to route notifications to the corresponding clients, anOrdertable that will be used to keep track of the new orders, and aDeliveryOrdertable that will be used to keep track of the deliveries, with these commands:azure mobile table create[AzureMobileServiceName]Channelazure mobile table create[AzureMobileServiceName]Orderazure mobile table create[AzureMobileServiceName]DeliveryOrder
- Upload a script to your Windows Azure Mobile Service that will add the order messages to the Windows Azure Service Bus queues:
- From a command line, change to the
Scriptsdirectory in the sample'sStoreSalesproject. - Open
order.insert.jsin theTablefolder and replace "namespace name" and "namespace key" with theServiceBusNamespaceIdentifierand default key from step 5. - Run this command:
azure mobile script upload[AzureMobileServiceName]table/order.insert.js
- From a command line, change to the
- Create a scheduled job in Mobile Services that will dequeue the orders from your Windows Azure Service Bus queue, process the orders, insert the processed order in the
DeliveryOrdertable, and send a notification to the clients that there are new orders, with these commands:azure mobile job create[AzureMobileServiceName]processOrderazure mobile job update[AzureMobileServiceName]processOrder --status enabled
- Update the created scheduled job with the script that it needs to run when called:
- From a command line, change to the
Scriptsdirectory in the sample'sStoreSalesproject. - Open
processOrder.jsin theSchedulerfolder and replace "namespace name" and "namespace key" with theServiceBusNamespaceIdentifierand default key from step 5. - Run this command:
azure mobile script upload[AzureMobileServiceName]scheduler/processOrder.js
- From a command line, change to the
- Get the
ApplicationUrlandApplicationKeyfor your Windows Azure Mobile Service with this command:azure mobile show[AzureMobileServiceName] - Install the Windows Azure Mobile Services NuGet package. Make sure that you are adding NuGet references to both of the projects in the solution. If you are using the
Library Package Manager Console in Visual Studio, you must run the following commands:
PM> Install-Package WindowsAzure.MobileServicesPM> Install-Package WindowsAzure.MobileServices -Project StoreRoom
- Open the
App.xaml.csfile and replace "mobile-service-url" and "mobile-service-key" with theApplicationUrlandApplicationKey. Your Windows Store app is now configured to communicate with your created Windows Azure Mobile Service. - Click Build > Build Solution.
Run the sample
- To debug the app and then run it in Visual Studio, press F5 or use Debug > Start Debugging. To run the app without debugging, press Ctrl+F5 or use Debug > Start Without Debugging.
- Open the
StoreSalesapp and complete the form by adding the customer name, product, and amount, and click Purchase. - Open the
StoreRoomapp, which will register a channel with your Windows Azure Mobile Service to receive notifications.
You can now wait for your scheduled job to run and process the order (it runs every 15 minutes by default), or you can sign in to the
Windows Azure management portal, select the
Scheduler tab of your Windows Azure Mobile Service, and click Run Once. You should then receive a toast notification informing you that a new order has been placed. When you click on the notification, you will see that the
Storeroom data has been refreshed to show you the recently-placed order.